package cn.idongjia.kuaidi.service.impl;

import cn.idongjia.kuaidi.common.MongoUtils;
import cn.idongjia.kuaidi.mapper.ShipMapper;
import cn.idongjia.kuaidi.pojos.Response;
import cn.idongjia.kuaidi.pojos.TrackMessage;
import cn.idongjia.kuaidi.repository.KdRepo;
import cn.idongjia.kuaidi.service.KdService;
import cn.idongjia.log.Log;
import cn.idongjia.log.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MultivaluedMap;

/**
 * Created by dongjia_lj on 16/4/8.
 */
@Path("/")
@Produces({"application/json;charset=UTF-8"})
public class KdServiceImpl implements KdService {
    private static final Log LOGGER = LogFactory.getLog(KdServiceImpl.class);
    @Autowired
    KdRepo kdRepo;
    @Autowired
    ShipMapper shipMapper;

    @Override
    @Path("notify")
    @POST
    @Consumes("application/x-www-form-urlencoded")
    public int notify(MultivaluedMap<String, String> params) {
        if (null != params) {
            try {
//                TrackMessage trackMessage = Utils.toPojo(params, TrackMessage.class);
//                Track track = GSON.fromJson(trackMessage.getTracklist(), Track.class);
                MongoUtils.updateTrack(params.getFirst("outid"), params.getFirst("tracklist"));
            } catch (Exception e) {
                LOGGER.error(e.getMessage(), e);
            }
        }
        return 0;
    }


    @Path("getTrace/{id}")
    @GET
    public Response getMessage(@PathParam("id") String ooid) {
        Response response = new Response(500);
        try {
            TrackMessage result = MongoUtils.findone(ooid);
            response.setRes(result);
            response.setCode(0);
        } catch (Exception e) {
            response.setMsg(e.getMessage());
            LOGGER.error(e.getMessage(), e);
        }
        return response;
    }

    @Override
    @Path("subscribe")
    @POST
    @Consumes({"application/json;charset=UTF-8"})
    public Response subscribe(String ooid,String outid, String companyname) {
        Response res = new Response(500);
        try {
            kdRepo.subScribe(ooid,outid, companyname);
            res.setCode(0);
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            res.setMsg(e);

        }
        return res;
    }

}
